package com.honghu.villagepost.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.honghu.villagepost.R
import com.honghu.villagepost.activity.fragment.GoodsFragment
import com.honghu.villagepost.activity.fragment.StoreDetailsFragment
import com.honghu.villagepost.adapter.BaseFragmentStateAdapter
import com.honghu.villagepost.api.ApiService
import com.honghu.villagepost.base.BaseActivity
import com.honghu.villagepost.callback.DialogCallback
import com.honghu.villagepost.callback.LzyResponse
import com.honghu.villagepost.constant.Constants
import com.honghu.villagepost.entity.Store
import com.honghu.villagepost.util.DensityUtil
import com.honghu.villagepost.util.GlideUtil
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_store_details.*


/**
 * @author: mqwang
 * @create: 2/2/21 3:42 PM
 * @Description: 店铺详情
 */
class StoreDetailsActivity : BaseActivity() {

    private val mTitles: MutableList<String> = ArrayList()
    private val tabFragmentList: MutableList<Fragment> = ArrayList()

    companion object {
        fun self(context: Context, shopId: Int, cid: Int) {
            val intent = Intent(context, StoreDetailsActivity::class.java)
            intent.putExtra(Constants.SHOP_ID, shopId)
            intent.putExtra(Constants.CID, cid)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_store_details
    }

    override fun init(savedInstanceState: Bundle?, container: View?) {
        setMainTitle("商家")

        var shopId = intent.getIntExtra(Constants.SHOP_ID, 0)
        var cid = intent.getIntExtra(Constants.CID, 0)

        mTitles.add("商品")
        mTitles.add("商家")

        tabFragmentList.add(GoodsFragment.newInstance(shopId, cid)!!)
        tabFragmentList.add(StoreDetailsFragment.newInstance(shopId)!!)

        view_pager.offscreenPageLimit = tabFragmentList.size
        view_pager.setAdapter(BaseFragmentStateAdapter(supportFragmentManager, tabFragmentList, mTitles))
        tab_layout.setupWithViewPager(view_pager) //此方法就是让tablayout和ViewPager联动

        ApiService.shop_id(this, shopId!!, object : DialogCallback<LzyResponse<Store>>() {
            override fun onSuccess(response: Response<LzyResponse<Store>>?) {
                var store = response!!.body().data
                updateStoreDetail(store)
            }
        })
    }

    fun updateStoreDetail(store: Store) {
        tv_store_name.setText(store.name)
        GlideUtil.loadImg(iv_image, store.logo, DensityUtil.dp2px(8f))
        tv_intro.setText(store.intro)
    }
}